﻿using MockingBird.Common.Infrastructure.Logging;

namespace MockingBird.Studio.UI.Helpers
{
    using System.Diagnostics;
    using Microsoft.Practices.Unity;
    using MockingBird.Common.Infrastructure.DependencyResolution;
    using MockingBird.Simulator.Framework.Configuration;
    using MockingBird.Simulator.Management;

    internal static class DependencyHelper
    {
        private static IUnityContainer managementContainer;
        private static bool isDefaultConfigured;
        private static bool isManagementConfigured;

        static DependencyHelper()
        {
            ConfigureContainers();
        }

        private static void ConfigureContainers()
        {
            DependencyResolver.ConfigurationFile = System.Configuration.ConfigurationManager.AppSettings["mockingbird_configuration_folder"];
            DependencyResolver.ConfigureDefaultContainer();
            managementContainer = DependencyResolver.DefaultContainer.CreateChildContainer();
            DependencyResolver.ConfigureContainer(managementContainer, "Management");
            isDefaultConfigured = true;
            isManagementConfigured = true;

        }

        public static void ForceConfigurationReload()
        {
            Trace.WriteLine("FORCE CONFIGURATION RELOAD");
            ConfigureContainers();
            string unityConfigFile = DependencyResolver.ConfigurationFile;
            Trace.WriteLine("Refreshed unity path : " + unityConfigFile);

        }


        internal static IServiceBuilder GetServiceBuilder(ServiceConfigurationSettings configSettings)
        {
            if (!isManagementConfigured)
            {
                ConfigureContainers();
            }
            managementContainer.RegisterInstance(typeof(ServiceConfigurationSettings), configSettings);
            return managementContainer.Resolve<IServiceBuilder>();
        }

        internal static IServiceBuilder GetServiceBuilder(IFolderManager folderManager, IHandlerConfigurationManager handlerConfigurationManager, ApplicationConfiguration applicationConfiguration, ServiceConfigurationSettings configSettings)
        {
            DependencyResolver.DefaultContainer.RegisterInstance<ApplicationConfiguration>(applicationConfiguration);
            managementContainer.RegisterInstance<IFolderManager>(folderManager);
            managementContainer.RegisterInstance<IHandlerConfigurationManager>(handlerConfigurationManager);
            managementContainer.RegisterInstance(typeof(ServiceConfigurationSettings), configSettings);
            return managementContainer.Resolve<IServiceBuilder>();

        }

        internal static IHandlerConfigurationManager GetHandlerConfigManager(ApplicationConfiguration simulationConfig)
        {
            if (!isManagementConfigured)
            {
                ConfigureContainers();
            }

            LoggingConfigurationProvider lcp = new LoggingConfigurationProvider();
            DependencyResolver.DefaultContainer.RegisterInstance<LoggingConfigurationProvider>(lcp);

            return managementContainer.Resolve<IHandlerConfigurationManager>();
        }

        internal static ApplicationConfiguration GetAppConfig()
        {
            if (!isDefaultConfigured)
            {
                ConfigureContainers();
            }
            return DependencyResolver.DefaultContainer.Resolve<ApplicationConfiguration>();

        }

        internal static IFolderManager GetFolderManager()
        {
            if (!isManagementConfigured)
            {
                ConfigureContainers();
            }
            return managementContainer.Resolve<IFolderManager>();

        }

        internal static ILogger GetLogger()
        {
            if (!isDefaultConfigured)
            {
                ConfigureContainers();
            }
            LoggingConfigurationProvider lcp = new LoggingConfigurationProvider();
            DependencyResolver.DefaultContainer.RegisterInstance<LoggingConfigurationProvider>(lcp);
            return DependencyResolver.DefaultContainer.Resolve<ILogger>();
        }

    }
}
